package com.joshjung.hover.net
{
    import com.joshjung.collections.LLIterator;
    import com.joshjung.collections.LinkedList;
    import com.joshjung.echosurf.ESPacket;
    import com.joshjung.echosurf.ESSocket;
    import com.joshjung.echosurf.IClientable;
    import com.joshjung.hover.interactive.HoverCraft;
    import com.joshjung.hover.interfaces.IByteArrayable;
    import com.joshjung.hover.interfaces.IKeyboardResponder;

    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.ProgressEvent;
    import flash.net.Socket;
    import flash.utils.ByteArray;
    import flash.utils.Endian;


    /**
     * This simple socket handles communication between clients. It extends
     * EchoServerSocket, which manages all the internal protocol.
     *
     * @author jjung
     */
    public class HoverSocket extends ESSocket implements IKeyboardResponder
    {
        public static const KEYDOWN_BYTE : int = 56;
        public static const KEYUP_BYTE : int = 57;

        public static const SERVER_SIGNATURE : int = 43;
        public static const PEER_SIGNATURE : int = 44;

        //---------------------------------------------------------------------
        //
        //  Constructor
        //
        //---------------------------------------------------------------------
        /**
         *  Constructor
         */
        public function HoverSocket(host : String, port : int)
        {
            super(host, port);
        }

        //---------------------------------------------------------------------
        //
        //  Methods
        //
        //---------------------------------------------------------------------
        public function serverSendAll(ll : LinkedList) : void
        {
            var ba : ByteArray = new ByteArray();
            ba.endian = Endian.LITTLE_ENDIAN;

            ba.writeByte(SERVER_SIGNATURE);

            var iterator : LLIterator = new LLIterator(ll);
            var data : *;

            while (data = iterator.next())
            {
                var sendable : IByteArrayable = data as IByteArrayable;

                if (sendable)
                {
                    //ba.writeInt(sendable.type);
                    ba.writeByte((sendable as IClientable).clientID);

                    sendable.toBA(ba);
                }
            }

            // Server sends to everyone except ourself (0 indicates a destination of to all)
            sendPacket(new ESPacket(ba, 0));
        }

        public function keyDown(keyCode : uint) : void
        {
            var ba : ByteArray = new ByteArray();
            ba.endian = Endian.LITTLE_ENDIAN;

            ba.writeByte(PEER_SIGNATURE);
            // Each Float is 4 bytes
            ba.writeByte(KEYDOWN_BYTE);
            ba.writeInt(keyCode);

            sendPacket(new ESPacket(ba, serverID));

            trace(" <-- " + KEYDOWN_BYTE + " for " + keyCode);
        }

        public function keyUp(keyCode : uint) : void
        {
            var ba : ByteArray = new ByteArray();
            ba.endian = Endian.LITTLE_ENDIAN;

            ba.writeByte(PEER_SIGNATURE);

            // Each Float is 4 bytes
            ba.writeByte(KEYUP_BYTE);
            ba.writeInt(keyCode);

            sendPacket(new ESPacket(ba, serverID));

            trace(" <-- " + KEYUP_BYTE + " for " + keyCode);
        }
    }
}
